package com.zhongshengnetwork.rightbe.common;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.MyActivityManager;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.dbmodel.GroupUserdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.GroupdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.NotifydbModel;
import com.zhongshengnetwork.rightbe.dbservice.GroupUserdbService;
import com.zhongshengnetwork.rightbe.dbservice.GroupdbService;
import com.zhongshengnetwork.rightbe.dbservice.NotifydbService;
import com.zhongshengnetwork.rightbe.gsonmodel.ClassifyModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GroupInfoModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GroupUserModel;
import com.zhongshengnetwork.rightbe.gsonmodel.JoinGroupModel;
import com.zhongshengnetwork.rightbe.gsonmodel.LifeResultModel;
import com.zhongshengnetwork.rightbe.gsonmodel.RedBackModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuInfoDetailModel;
import com.zhongshengnetwork.rightbe.gsonmodel.XHTGroupCreateModel;
import com.zhongshengnetwork.rightbe.gsonmodel.XHTGroupJoinModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.UserLangActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.ClassifyActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTListOfClassifyActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * Created by zhongsheng-luojianlan on 16/11/11.
 */

public class RCCommandUtils {

    public static boolean isAsyncGroupInfo(String groupid){
        String asynTime = GroupdbService.getAsyncTime(CustomApplication.loginModel.getUid()+groupid);
        if (asynTime == null || asynTime.equals("0")) {//还没有同步群成员信息
            return false;
        }
        return true;
    }

    //获取群成员信息
    public static void getGroupMember(String groupid){
        String asynTime = GroupdbService.getAsyncTime(CustomApplication.loginModel.getUid()+groupid);
        if (asynTime == null || asynTime.equals("0")){//还没有同步群成员信息
            final Map<String,String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            map.put(APIKey.groupidKey,groupid);
            HttpsUtils.getMemberDo(map, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){

                    }else {
                        CustomApplication.showTip(commonModel, MyActivityManager.getInstance().getCurrentActivity());
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    //同步群信息
    public static void asyncGroupInfo(final String groupid){
        if (GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid) == null){
            final Map<String,String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            map.put(APIKey.groupidKey,groupid);
            HttpsUtils.getGroupInfoDo(map, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        GroupInfoModel groupInfoModel = GsonTools.getGroupInfoModel(data);
                        if (groupInfoModel != null){
                            GroupdbModel groupdbModel = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupInfoModel.getGroupid());
                            if (groupdbModel == null){
                                groupdbModel = new GroupdbModel();
                                groupdbModel.setId(CustomApplication.loginModel.getUid()+groupInfoModel.getGroupid());
                                groupdbModel.setUserid(CustomApplication.loginModel.getUid());
                                groupdbModel.setGroupid(groupInfoModel.getGroupid());
                                groupdbModel.setOwnerid(groupInfoModel.getGroupownerid());
                                groupdbModel.setName(groupInfoModel.getGroupname());
                                groupdbModel.setHeader(groupInfoModel.getGroupheader());
                                groupdbModel.setAsyntime("0");
                                groupdbModel.setAuth(Integer.valueOf(groupInfoModel.getGroupauthstate()));
                                groupdbModel.setReport(groupInfoModel.getGroupreport());
                                groupdbModel.setState("0");
                            }else {
                                groupdbModel.setName(groupInfoModel.getGroupname());
                                groupdbModel.setHeader(groupInfoModel.getGroupheader());
                                groupdbModel.setAuth(Integer.valueOf(groupInfoModel.getGroupauthstate()));
                                groupdbModel.setReport(groupInfoModel.getGroupreport());
                            }
                            GroupdbService.saveOrUpdate(groupdbModel);
                        }
                        //同步群成员信息
                        getGroupMember(groupid);
                    }else {
                        CustomApplication.showTip(commonModel, MyActivityManager.getInstance().getCurrentActivity());
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    //小灰条消息
    public static String xiaoHuiTiaoInfo(String string){
        String type = GsonTools.getXiaoHuiTiaoModel(string);
        //System.out.print("处理小灰条消息\n");
        if (type != null){
            if (type.equals(Constant.XHTMessageType.Group_Create)){//创建群消息
                //System.out.print("创建群消息\n");
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    //同步群成员信息
                    List<String> userIds = CommonUtils.stringToArrayList(xhtGroupCreateModel.getUserid());
                    List<String> headers = CommonUtils.stringToArrayList(xhtGroupCreateModel.getHeader());
                    List<String> names = CommonUtils.stringToArrayList(xhtGroupCreateModel.getName());
                    if (userIds == null && userIds.size() == 0){
                        return null;
                    }
                    if (GroupdbService.getModel(CustomApplication.loginModel.getUid()+xhtGroupCreateModel.getGroupid()) == null){
                        GroupdbModel groupdbModel = new GroupdbModel();
                        groupdbModel.setId(CustomApplication.loginModel.getUid()+xhtGroupCreateModel.getGroupid());
                        groupdbModel.setOwnerid(userIds.get(0));
                        groupdbModel.setAsyntime("0");
                        groupdbModel.setReport("");
                        groupdbModel.setHeader(xhtGroupCreateModel.getGroupheader());
                        groupdbModel.setName(xhtGroupCreateModel.getGroupname());
                        groupdbModel.setGroupid(xhtGroupCreateModel.getGroupid());
                        groupdbModel.setState("0");
                        groupdbModel.setAuth(1);
                        GroupdbService.saveOrUpdate(groupdbModel);
                    }
                    long time = Long.valueOf(xhtGroupCreateModel.getTime());

                    for (int i = 0; i < userIds.size(); i++){
                        GroupUserdbModel model = new GroupUserdbModel();
                        model.setId(xhtGroupCreateModel.getGroupid()+userIds.get(i));
                        model.setGroupid(xhtGroupCreateModel.getGroupid());
                        model.setUserid(userIds.get(i));
                        if (i == 0){
                            model.setType(Constant.ownerType);
                        }else {
                            model.setType(Constant.memberType);
                        }
                        model.setName(names.get(i));
                        model.setNickname(names.get(i));
                        model.setHeader(headers.get(i));
                        model.setTime(time);
                        time += 1;
                        GroupUserdbService.saveOrUpdate(model);
                    }
                    GroupdbService.setGroupTime(System.currentTimeMillis()+"",CustomApplication.loginModel.getUid()+xhtGroupCreateModel.getGroupid());
                }
                //以上创建群消息
            }else if (type.equals(Constant.XHTMessageType.Group_Join)){//加人群
                XHTGroupJoinModel xhtGroupJoinModel = GsonTools.getXHTGroupJoinModel(string);
                if (xhtGroupJoinModel != null){
                    List<String> userIds = CommonUtils.stringToArrayList(xhtGroupJoinModel.getUserid());
                    List<String> headers = CommonUtils.stringToArrayList(xhtGroupJoinModel.getHeader());
                    List<String> names = CommonUtils.stringToArrayList(xhtGroupJoinModel.getName());
                    List<String> times = CommonUtils.stringToArrayList(xhtGroupJoinModel.getTime());
                    String groupid = xhtGroupJoinModel.getGroupid();
                    if (userIds == null && userIds.size() == 0){
                        return null;
                    }
                    //同步群信息
                    if (isAsyncGroupInfo(groupid) == false){
                        asyncGroupInfo(groupid);
                        return null;
                    }

                    //同步群成员信息
                    for (int i = 0; i < userIds.size(); i++){
                        GroupUserdbModel model = new GroupUserdbModel();
                        long time = Long.valueOf(times.get(i));
                        model.setId(groupid+userIds.get(i));
                        model.setGroupid(groupid);
                        model.setUserid(userIds.get(i));
                        model.setType(Constant.memberType);
                        model.setName(names.get(i));
                        model.setNickname(names.get(i));
                        model.setHeader(headers.get(i));
                        model.setTime(time);
                        time += 1;
                        GroupUserdbService.saveOrUpdate(model);
                        if (userIds.get(i).equals(CustomApplication.loginModel.getUid())){
                            GroupdbModel groupdbModel = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                            if (groupdbModel != null){
                                groupdbModel.setState("0");
                                GroupdbService.saveOrUpdate(groupdbModel);
                            }
                        }
                    }
                    return groupid;
                }
                //以上加人群
            }else if (type.equals(Constant.XHTMessageType.Group_Delete)){//移除出群
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    //同步群成员信息
                    List<String> userIds = CommonUtils.stringToArrayList(xhtGroupCreateModel.getUserid());
                    if (userIds == null && userIds.size() == 0){
                        return null;
                    }
                    String groupid = xhtGroupCreateModel.getGroupid();
                    for (String userid : userIds){
                        String id = groupid+userid;
                        GroupUserdbService.deleteGroupUser(id);
                        if (userid.equals(CustomApplication.loginModel.getUid())){//自己被移除了
                            GroupdbModel model = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                            if (model != null){
                                model.setState("2");
                                GroupdbService.saveOrUpdate(model);
                            }
                        }
                    }
                    return groupid;
                }
                //以上是移除出群
            }else if (type.equals(Constant.XHTMessageType.Group_quit)){//退出群
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    List<String> userIds = CommonUtils.stringToArrayList(xhtGroupCreateModel.getUserid());
                    if (userIds == null && userIds.size() == 0){
                        return null;
                    }
                    String groupid = xhtGroupCreateModel.getGroupid();
                    for (String userid : userIds){
                        String id = groupid+userid;
                        GroupUserdbService.deleteGroupUser(id);
                    }
                    if (!CommonUtils.isEmpty(xhtGroupCreateModel.getGroupownerid())){
                        GroupdbModel groupdbModel = GroupdbService.getModel(CustomApplication.loginModel.getUid()+xhtGroupCreateModel.getGroupid());
                        if (groupdbModel != null){
                            groupdbModel.setOwnerid(xhtGroupCreateModel.getGroupownerid());
                            GroupdbService.saveOrUpdate(groupdbModel);
                        }
                        GroupUserdbModel model = GroupUserdbService.getdbUserModel(groupid+xhtGroupCreateModel.getGroupownerid());
                        if (model != null){
                            model.setType(Constant.ownerType);
                            GroupUserdbService.saveOrUpdate(model);
                            return groupid;
                        }
                    }
                }
                //以上是退出群
            }else if (type.equals(Constant.XHTMessageType.Group_quit)){//群解散
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    String groupid = xhtGroupCreateModel.getGroupid();
                    GroupdbModel model = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                    if (model != null){
                        model.setState("2");
                        GroupdbService.saveOrUpdate(model);
                    }
                }
                //以上是群解散
            }else if (type.equals(Constant.XHTMessageType.Group_MemberName)){//群成员修改昵称
                //以上是群成员修改昵称
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    String groupid = xhtGroupCreateModel.getGroupid();
                    //同步群信息
                    if (isAsyncGroupInfo(groupid) == false){
                        asyncGroupInfo(groupid);
                        return null;
                    }

                    String userid = xhtGroupCreateModel.getUserid();
                    String groupusername = xhtGroupCreateModel.getGroupusername();
                    GroupUserdbModel model = GroupUserdbService.getdbUserModel(groupid+userid);
                    if (model != null){
                        model.setName(groupusername);
                        GroupUserdbService.saveOrUpdate(model);
                    }
                }
            }else if (type.equals(Constant.XHTMessageType.Group_Report)){//群公告
                //以上是群公告
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    String report = xhtGroupCreateModel.getGroupreport();
                    String groupid = xhtGroupCreateModel.getGroupid();

                    //同步群信息
                    if (isAsyncGroupInfo(groupid) == false){
                        asyncGroupInfo(groupid);
                        return null;
                    }

                    GroupdbModel model = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                    if (model != null){
                        model.setReport(report);
                        GroupdbService.saveOrUpdate(model);
                        return groupid;
                    }
                }
            }else if (type.equals(Constant.XHTMessageType.Group_Name)){//群名称
                //以上是群名称
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    String groupname = xhtGroupCreateModel.getGroupname();
                    String groupid = xhtGroupCreateModel.getGroupid();

                    //同步群信息
                    if (isAsyncGroupInfo(groupid) == false){
                        asyncGroupInfo(groupid);
                        return null;
                    }

                    GroupdbModel model = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                    if (model != null){
                        model.setName(groupname);
                        GroupdbService.saveOrUpdate(model);
                        return groupid;
                    }
                }
            }else if (type.equals(Constant.XHTMessageType.Group_Auth)){//群状态
                //以上是群状态
                XHTGroupCreateModel xhtGroupCreateModel = GsonTools.getXHTGroupCreateModel(string);
                if (xhtGroupCreateModel != null){
                    String authstate = xhtGroupCreateModel.getGroupauthstate();
                    String groupid = xhtGroupCreateModel.getGroupid();

                    //同步群信息
                    if (isAsyncGroupInfo(groupid) == false){
                        asyncGroupInfo(groupid);
                        return null;
                    }

                    GroupdbModel model = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
                    if (model != null){
                        model.setAuth(Integer.valueOf(authstate));
                        GroupdbService.saveOrUpdate(model);
                        return groupid;
                    }
                }
            }


        }
        return null;
    }

    //同步群成员
    public static String asyncGroupMember(String dataString){
        try {
            JSONObject jsonObject = CommonUtils.stringToJson(dataString);
            String groupid = jsonObject.getString(APIKey.groupidKey);

            String data = jsonObject.getString(APIKey.dataKey);
            JSONArray jsonArray = CommonUtils.stringToJsonArray(data);
            if (jsonArray != null && jsonArray.length() > 0){
                for (int i = 0; i < jsonArray.length(); i++){
                    Gson mgson = new Gson();
                    String string = jsonArray.getString(i);
                    GroupUserModel groupUserModel = mgson.fromJson(
                            string, GroupUserModel.class);
                    GroupUserdbModel model = new GroupUserdbModel();
                    model.setId(groupid+groupUserModel.getUserid());
                    model.setGroupid(groupid);
                    model.setUserid(groupUserModel.getUserid());
                    model.setType(Integer.valueOf(groupUserModel.getType()));
                    model.setName(groupUserModel.getGroupusername());
                    model.setNickname(groupUserModel.getNickname());
                    model.setHeader(groupUserModel.getHeader());
                    model.setTime(Long.valueOf(groupUserModel.getTime()));
                    GroupUserdbService.saveOrUpdate(model);
                }
                GroupdbService.setGroupTime(System.currentTimeMillis()+"", CustomApplication.loginModel.getUid()+groupid);
                return groupid;
            }

            return null;

        }catch (Exception e){
            return null;
        }
    }

    //更新团长
    public static String changeGroupOwner(String dataString){
        try {
            JSONObject jsonObject = CommonUtils.stringToJson(dataString);
            String groupid = jsonObject.getString(APIKey.groupidKey);
            String userid = jsonObject.getString(APIKey.useridKey);
            GroupUserdbModel model = GroupUserdbService.getdbUserModel(groupid+userid);
            if (model != null){
                model.setType(Constant.memberType);
                GroupUserdbService.saveOrUpdate(model);
            }

            model = GroupUserdbService.getdbUserModel(groupid+CustomApplication.loginModel.getUid());
            if (model != null){
                model.setType(Constant.ownerType);
                GroupUserdbService.saveOrUpdate(model);
            }
            GroupdbModel groupdbModel = GroupdbService.getModel(CustomApplication.loginModel.getUid()+groupid);
            if (groupdbModel != null){
                groupdbModel.setOwnerid(CustomApplication.loginModel.getUid());
                groupdbModel.setHeader(CustomApplication.loginModel.getHeader());
                GroupdbService.saveOrUpdate(groupdbModel);
            }

            return groupid;

        }catch (Exception e){
            return null;
        }
    }

    /*
     * 读取联系人的信息
     */
    public static void testReadAllContacts() {
        Cursor cursor = CustomApplication.mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        int contactIdIndex = 0;
        int nameIndex = 0;

        if(cursor.getCount() > 0) {
            contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        }
        while(cursor.moveToNext()) {
            String contactId = cursor.getString(contactIdIndex);
            String name = cursor.getString(nameIndex);
            System.out.print("ID:"+contactId+"\n");
            System.out.print("名字："+name+"\n");
            /*
             * 查找该联系人的phone信息
             */
            Cursor phones = CustomApplication.mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                    null, null);
            int phoneIndex = 0;
            if(phones.getCount() > 0) {
                phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            }
            while(phones.moveToNext()) {
                String phoneNumber = phones.getString(phoneIndex);
                System.out.print("手机号："+phoneNumber+"\n");
            }

            /*
             * 查找该联系人的email信息
             */
            Cursor emails = CustomApplication.mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
                    null, null);
            int emailIndex = 0;
            if(emails.getCount() > 0) {
                emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
            }
            while(emails.moveToNext()) {
                String email = emails.getString(emailIndex);
                System.out.print("邮箱："+email+"\n");
            }

        }
    }

    //微句审核
    public static void dealWithLifeResult(String data, Context context, long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(Integer.valueOf(Constant.MessageType.Life_Result));
            String content = lifeResultModel.getName();
            model.setContent(content);
            model.setDict(data);
            model.setTime(sendtime);
            model.setMsgtype(Constant.MessageListType.DefaultMsgType);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }

    //处理关注消息
    public static void dealWithCare(String data, Context context, long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(Integer.valueOf(Constant.MessageType.LifeLike_Result));
            String content = lifeResultModel.getName();
            model.setContent(content);
            model.setDict(data);
            model.setTime(sendtime);
            model.setMsgtype(Constant.MessageListType.CareMsgType);
            Log.e("TAG","保存时间是："+sendtime);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }

    //处理生活录相关通知
    public static void dealWithLife(String data, int type, Context context,  long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(type);
            String content = lifeResultModel.getName();
            model.setContent(content);
            if (model.getContent().contains("轮播推荐") || model.getContent().contains("微句被收录")){
                //model.setType(Integer.valueOf(Constant.MessageType.Lang_Banner_Notify));
                model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            }else if (model.getContent().contains("评论被收录")){
                //model.setType(Integer.valueOf(Constant.MessageType.Comment_Collection_Notify));
                model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            }else if(type == Integer.valueOf(Constant.MessageType.LangPrize_Result)){
                model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            } else {
                if (type == Integer.valueOf(Constant.MessageType.LifeC_Result)
                 || type == Integer.valueOf(Constant.MessageType.LifeCR_Result)){
                    model.setMsgtype(Constant.MessageListType.CommentMsgType);
                }else {
                    model.setMsgtype(Constant.MessageListType.DefaultMsgType);
                }
            }

            model.setDict(data);
            model.setTime(sendtime);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }

    //处理系统通知
    public static void dealWithSystem(String data, int type, Context context,  long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(type);
            String content = lifeResultModel.getName();
            model.setContent(content);
            model.setDict(data);

            model.setTime(sendtime);
            model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);

            try {
                JSONObject dataJson = CommonUtils.stringToJson(model.getDict());
                if (CommonUtils.isLogin() == true){
                    if (dataJson.has("level")){
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.UserLevelNameData+CustomApplication.loginModel.getUid(), CommonUtils.formatString(dataJson.getString("level")));
                    }
                }
            }catch (Exception e){

            }

        }
    }

    //处理二维码处理结果
    public static final void dealWithQRCode(String content, final Activity activity){
        int startIndex = content.indexOf("?")+1;
        String data = content.substring(startIndex,content.length());
        try {
            Log.e("TAG", "解密前="+data);
            data = Des.decryptDES(data,Constant.DESKey);
            Log.e("TAG", "解密后="+data);
            final com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(data);
            if (!CommonUtils.isEmpty(jsonObject.getString(APIKey.useridKey))){//用户二维码
                String userid = jsonObject.getString(APIKey.useridKey);
                Intent intent = new Intent(activity, UserLangActivity.class);
                intent.putExtra(APIKey.titleKey,"Ta的主页");
                intent.putExtra(APIKey.useridKey,userid);
                activity.startActivity(intent);

            }else {

            }
        }catch (Exception e){

        }
    }

    //处理微纸条二维码处理结果
    public static final void dealWithWZTQRCode(String content, final Activity activity){
        int startIndex = content.indexOf("?")+1;
        String data = content.substring(startIndex,content.length());
        try {
            Log.e("TAG", "解密前="+data);
            data = Des.decryptDES(data,Constant.DESKey);
            Log.e("TAG", "解密后="+data);
            final com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(data);
            if (!CommonUtils.isEmpty(jsonObject.getString("weishuId"))){//微纸条二维码
                String weishuId = jsonObject.getString("weishuId");
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.idKey, weishuId);
                if (CommonUtils.isLogin() == true){
                    map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
                }
                String url = "weishu/theme/info.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            WeiShuInfoDetailModel langInfoModel = GsonTools.getWeiShuInfo(data);
                            Intent intent = new Intent(activity, WZTActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", langInfoModel);//序列化
                            intent.putExtras(bundle);//发送数据
                            intent.putExtra("selectIndex",0);
                            activity.startActivity(intent);
                        }else {
                            CustomApplication.showTip(commonModel, activity);
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }else {

            }
        }catch (Exception e){

        }
    }

    //处理微纸条分类二维码
    public static final void dealWithClassifyQRCode(String content, final Activity activity){
        int startIndex = content.indexOf("?")+1;
        String classifyId = content.substring(startIndex,content.length());
        try {
            if (!CommonUtils.isEmpty(classifyId)){//分类二维码
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put("dataId", Des.decryptDES(classifyId, Constant.DESKey));
                if (CommonUtils.isLogin() == true){
                    map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
                }
                String url = "weishu/classify/info.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            ClassifyModel model = GsonTools.getClassifyModelOne(data);
                            if (GsonTools.getClassifyNeedAuth(data) == true){
                                CustomDialog dialog = null;
                                final CustomDialog.Builder builder = new CustomDialog.Builder(activity);
                                builder.setTitle("温馨提示");
                                builder.setMessage("该分类需要作者授权才能查看，是否现在提交申请？");
                                builder.setPositiveButton("申请授权", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        final HashMap<String, String> map = new HashMap<String, String>();
                                        map.put("dataId", model.getClassifyId()+"");
                                        if (CommonUtils.isLogin() == true){
                                            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
                                        }
                                        String url = "weishu/classify/request/auth.do";
                                        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                                            @Override
                                            public void onSuccess(String data) {
                                                CommonModel commonModel1 = GsonTools.getCommonModel(data);
                                                if (commonModel1.getFlag().equals("1")){
                                                    ToastUtil.show(activity,"提交申请成功，作者授权后即可查看");
                                                }else {
                                                    ToastUtil.show(activity, commonModel1.getMsg());
                                                }
                                            }

                                            @Override
                                            public void onError(String msg) {
                                                ToastUtil.show(activity, "提交申请失败");
                                            }
                                        });
                                    }
                                });
                                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                dialog = builder.create();
                                dialog.show();
                            }else {
                                Intent intent = new Intent(activity, WZTListOfClassifyActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("model",model);
                                intent.putExtras(bundle);
                                activity.startActivity(intent);
                            }
                        }else {
                            CustomApplication.showTip(commonModel, activity);
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }else {

            }
        }catch (Exception e){

        }
    }

    //处理微纸条相关通知
    public static void dealWithWZT(String data, int type, Context context,  long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(type);
            String content = lifeResultModel.getName();
            model.setContent(content);

            if (type == Integer.valueOf(Constant.MessageType.WeiShu_Subscribe_Notify)){
                model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            }else if (type == Integer.valueOf(Constant.MessageType.WeiShu_Content_Comment_Notify)
                    || type == Integer.valueOf(Constant.MessageType.WeiShu_Comment_Notify)
                    || model.getContent().contains("#评论") == true){
                model.setMsgtype(Constant.MessageListType.CommentMsgType);
            }else {
                model.setMsgtype(Constant.MessageListType.DefaultMsgType);
                if (model.getContent().contains("评论被收录")){
                    //model.setType(Integer.valueOf(Constant.MessageType.Weishu_Content_C_Notify));
                    model.setMsgtype(Constant.MessageListType.NotifyMsgType);
                }
            }
            model.setDict(data);
            model.setTime(sendtime);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }

    //处理分类相关通知
    public static void dealWithClassify(String data, int type, Context context,  long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(type);
            model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            String content = lifeResultModel.getName();
            model.setContent(content);
            model.setDict(data);
            model.setTime(sendtime);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }

    //处理智慧排行信息
    public static void dealWithSmartRank(String data, int type, Context context,  long sendtime){
        LifeResultModel lifeResultModel = GsonTools.getLifeResultModel(data);
        if (lifeResultModel != null){
            NotifydbModel model = new NotifydbModel();
            model.setUserid(CustomApplication.loginModel.getUid());
            model.setState(0);
            model.setType(type);
            if (lifeResultModel.getType().equals("0")){
                model.setMsgtype(Constant.MessageListType.NotifyMsgType);
            }else {
                model.setMsgtype(Constant.MessageListType.DefaultMsgType);
            }
            String content = lifeResultModel.getName();
            model.setContent(content);
            model.setDict(data);
            model.setTime(sendtime);
            NotifydbService.saveOrUpdate(model);
            Intent mIntent = new Intent(BroadcastDefine.Notify_Msg);
            //发送广播
            context.sendBroadcast(mIntent);
        }
    }


}
